package net.monare.mifss.admin.ui.comp;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import net.monare.mifss.MifssMessages;
import net.monare.mifss.configurations.pojo.Property;
import net.monare.mifss.evn.AddPropertyListener;
import net.monare.mifss.evn.ProperyFieldDetachListener;

import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Runo;

public class PropertyListUi extends VerticalLayout {

	private static final long serialVersionUID = -6622636865323489L;

	private VerticalLayout propsLayout;
	private VerticalLayout labelLayout;

	private String keyWidth;
	private String valueWidth;

	public PropertyListUi(String listTitle, String keyWidth, String valueWidth) {

		this.keyWidth = keyWidth;
		this.valueWidth = valueWidth;

		propsLayout = new VerticalLayout();
		labelLayout = new VerticalLayout();

		setCaption(listTitle);

		AddPropertyListener addListener = new AddPropertyListener(this, keyWidth, valueWidth);

		NativeButton addPropButton = new NativeButton(MifssMessages.getMessage("button.add.new.property"));
		addPropButton.setWidth(keyWidth);

		addPropButton.addClickListener(addListener);

		addComponent(labelLayout);
		addComponent(propsLayout);
		addComponent(addPropButton);

	}

	public Set<Property> getPropertySet() {

        Set<Property> resultList = new HashSet<Property>();

		for (int i = 0; i < propsLayout.getComponentCount(); i++) {

			PropertyFieldUi propUi = (PropertyFieldUi) propsLayout.getComponent(i);

			if (!StringUtils.isEmpty(propUi.getKey()) && !StringUtils.isEmpty(propUi.getValue())) {
				resultList.add(new Property(propUi.getKey(), propUi.getValue()));
			}

		}

		return resultList;

	}

	public int getPropertyCount() {
		return propsLayout.getComponentCount();
	}

	public void addProperyUiField(PropertyFieldUi propertyField) {

		if (labelLayout.getComponentCount() == 0) {
			labelLayout.addComponent(getLabels());
		}

		ProperyFieldDetachListener dl = new ProperyFieldDetachListener(this);
		propertyField.addDetachListener(dl);

		propsLayout.addComponent(propertyField);
	}

	private HorizontalLayout getLabels() {

		Label keyLabel = new Label(MifssMessages.getMessage("label.key"));
		keyLabel.setWidth(keyWidth);
		keyLabel.setStyleName(Runo.LABEL_SMALL);

		Label valueLabel = new Label(MifssMessages.getMessage("label.value"));
		valueLabel.setWidth(valueWidth);
		valueLabel.setStyleName(Runo.LABEL_SMALL);

		HorizontalLayout labels = new HorizontalLayout();
		labels.addComponent(keyLabel);
		labels.addComponent(valueLabel);

		return labels;
	}

	public void clearLabels() {
		labelLayout.removeAllComponents();
	}

	public void setProperties(Set<Property> properties) {

		// clear all properties
		propsLayout.removeAllComponents();
		
		// add new properties
		for (Property property : properties) {

			PropertyFieldUi propertyFieldUi = new PropertyFieldUi(keyWidth, valueWidth);

			propertyFieldUi.setPropertyKey(property.getName());
			propertyFieldUi.setPropertyValue((String) property.getValue());

			addProperyUiField(propertyFieldUi);
		}

	}
	
	
}
